#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        if (head == nullptr || head->next == nullptr) return head;
        ListNode* newhead = new ListNode;
        ListNode* pcur = head, * next = head->next, * prev = newhead, * nnext = next->next;

        while (next)
        {
            prev->next = next, pcur->next = nnext, next->next = pcur, prev = pcur, pcur = nnext;
            if (nnext) next = nnext->next;
            else next = nullptr;
            if (next) nnext = next->next;
        }
        pcur = newhead->next;
        delete newhead;
        return pcur;
    }
};